/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.wxs.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: wxshop - DemoController
 * <p>Powered by webrx On 2021-04-16 21:10:46
 * <p> 功能： 用于测试控制器
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Controller
public class DemoController {
    @GetMapping({"/demo"})
    public ModelAndView info() {
        ModelAndView mv = new ModelAndView("demo");
        mv.addObject("name", "<strong>李四</strong>");
        mv.addObject("age", 18);
        mv.addObject("color", "blue");
        return mv;
    }

    @GetMapping("/demo2")
    public String[] info2() {
        return new String[]{"java程序设计", "数据库mysql入门"};
    }

    @GetMapping("/demo3")
    public Date info3() {
        return new Date();
    }

    @GetMapping("/demo4")
    public Date info4() {
        int i = 3 / 0;
        return new Date();
    }

    @GetMapping("/demo5")
    public void info5() {
    }


    @PostMapping("/demo6")
    @ResponseBody
    public Map<String, Object> info5(@RequestParam("file") MultipartFile[] file, HttpSession s) throws IOException {
        //File p = new File(s.getServletContext().getRealPath("/user/abc/"));
        //p.mkdirs();

        for (MultipartFile f : file) {
            System.out.println(f.getName());
            System.out.println(f.getSize());
            System.out.println(f.getOriginalFilename());
            f.transferTo(new File("c:/",f.getOriginalFilename()));
        }




        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "上传成功了");
        Map<String, Object> fs = new HashMap<String, Object>();
        fs.put("src", "/asfasf/afasfa/2424242342.jpg");
        map.put("data", fs);
        return map;
    }

}
